/* string.c */

#include <stdlib.h>

size_t
my_strlen(const char *str)
{
  const char *s = str;
  while (*s)
    s++;
  return s-str;
}

char *
my_strcpy(char *dst, const char *src)
{
  while (*dst++ = *src++)
    ;
  return dst;
}

char *
my_strdup(const char *str)
{
  char *s = malloc(my_strlen(str)+1);
  if (s == NULL)
    return NULL;
  my_strcpy(s, str);
  return s;
}

char *
my_strchr(const char *s, int c)
{
  for (; *s; s++)
    if (*s == c)
      return (char *)s;
  if (c == '\0')
    return (char *)s;
  else
    return NULL;
}

char *
my_strrchr(const char *s, int c)
{
  const char *t;
  for(t=s; *t; t++)
    ;
  do {
    if (*t == c)
      return (char *)t;
    t--;
  } while (t != s);
  return NULL;
}

char *
my_strcat(char *s, const char *append)
{
  char *t = s;
  while (*t++)
    ;
  while (*append)
    *t++ = *append++;
  *t = '\0';
  return s;
}
